/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

//SMUIML v0.4


package it.unina.lab.smuil;

import it.unina.lab.costants.Constants;
import it.unina.lab.xml.Attribute;
import it.unina.lab.xml.HephaisTKParsingException;
import it.unina.lab.xml.XmlGeneric;

import java.util.Vector;


public class Recognizer extends Smuiml_Generic {
	/**
	 *
	 */
	private static final long serialVersionUID = -620929165695020118L;

	public final static String NAME = "recognizer";

	private Attribute attName = null;
	private Attribute attModality = null;
	private Vector<Parameter> parameters = null;
	private Vector<Variable> variables = null;
	private Vector<Translate_Value> translateValues = null;

	public Recognizer(){
		super();
		variables = new Vector<Variable>();
		translateValues = new Vector<Translate_Value>();
		parameters = new Vector<Parameter>();
		setName(NAME);
	}

	/**
	 * @return the attName
	 */
	public Attribute getAttName() {
		return attName;
	}

	public Attribute getAttModality() {
		return attModality;
	}

	/**
	 * @param attName the attName to set
	 */
	public void setAttModality(String attModality) {
		this.attModality = new Attribute("modality", attModality);
		attributes.add(this.attModality);
	}

	public void setAttName(String attName) {
		this.attName = new Attribute("name", attName);
		attributes.add(this.attName);
	}

	public void addVariable(Variable variable){
		variables.add(variable);
		elements.add(variable);
	}

	public Variable getVariable(int index){
		return variables.get(index);
	}

	public Vector<Variable> getVariables(){
		return variables;
	}

	public int variablesSize(){
		return variables.size();
	}
	
	public void addParameter(Parameter parameter){
		parameters.add(parameter);
		elements.add(parameter);
	}

	public Parameter getParameter(int index){
		return parameters.get(index);
	}

	public Vector<Parameter> getParameters(){
		return parameters;
	}

	public int parametersSize(){
		return parameters.size();
	}

	public void addTranslateValue(Translate_Value translateValue){
		translateValues.add(translateValue);
		elements.add(translateValue);
	}

	public Translate_Value getTranslateValue(int index){
		return translateValues.get(index);
	}

	public Vector<Translate_Value> getTranslateValues(){
		return translateValues;
	}

	public int translateValuesSize(){
		return translateValues.size();
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException {
		if(element instanceof Variable){
			addVariable((Variable)element);
		}else if(element instanceof Translate_Value){
			addTranslateValue((Translate_Value)element);
		}else if(element instanceof Parameter){
			addParameter((Parameter)element);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected element: "+element.getName());
		}
	}

	@Override
	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		super.addAttribute(attribute);
		String attName = attribute.getName();
		if (attName.indexOf(':') != -1) {
			attName = attName.substring(attName.indexOf(':')+1);
		}
		addAttribute(attName, attribute.getValue());
	}

	public void addAttribute(String name, String value) throws HephaisTKParsingException{
		if(name.equalsIgnoreCase("name")){
			setAttName(value);
		}else if(name.equalsIgnoreCase("modality")){
			setAttModality(value);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected attribute: "+name);
		}
	}

	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("<"+NAME+(attName != null? " name=\""+attName.getValue()+"\"" : "")+
				(attModality != null? " modality=\""+attModality.getValue()+"\"" : "")+">\n");
		for (int i = 0; i < variables.size(); i++){
			ret.append(variables.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for (int i = 0; i < translateValues.size(); i++){
			ret.append(translateValues.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for (int i = 0; i < parameters.size(); i++){
			ret.append(parameters.get(i).toString(indent + Constants.INDENT_PREFIX));
		}
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("</"+NAME+">\n");
		return ret.toString();
	}
}
